package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.converter.ShoppingGoodsConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.ShoppingGoodsDto;
import com.cskaoyan.shopping.dto.ShoppingGoodsListAndTotalDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IShoppingGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Author:WalterChan
 * @Decription:ShoppingGoodsServiceImpl
 * @Date Created in 2023-01-11-10:27
 */
@Service
public class ShoppingGoodsServiceImpl implements IShoppingGoodsService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ShoppingGoodsConverter shoppingGoodsConverter;

    private static Integer PRICE_MIN=-999;
    private static Integer PRICE_MAX=999999999;




    @Override
    public ResponseData shoppingGoods(PageRequest pageRequest) {
//        开启分页
        PageHelper.startPage(pageRequest.getPage(),pageRequest.getSize());

//        按照价格，顺序，cid查询出商品
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
//      检查是否需要顺序排序
       if (!Objects.equals(pageRequest.getSort(),null)){
           if (Objects.equals(pageRequest.getSort(),"1")){
               example.orderBy("price").asc();
           }
           if (Objects.equals(pageRequest.getSort(),"-1")){
               example.orderBy("price").desc();
           }
       }
//        默认查询价格从-999→999999999的商品
        BigDecimal priceMin=BigDecimal.valueOf(PRICE_MIN);
        BigDecimal priceMax=BigDecimal.valueOf(PRICE_MAX);
        if (pageRequest.getPriceGt()!=null){
            priceMin=pageRequest.getPriceGt();
        }
        if (pageRequest.getPriceLte()!=null){
            priceMax=pageRequest.getPriceLte();
        }
        criteria.andBetween("price",priceMin,priceMax);

//      默认没有传cid进来
        if (pageRequest.getCid()!=null) {
            criteria.andEqualTo("cid", pageRequest.getCid());
        }
//      这个是查出来的ShoppingGoodsDto
        List<Item> items = itemMapper.selectByExample(example);
        List<ShoppingGoodsDto> shoppingGoodsDtoList = shoppingGoodsConverter.converItem2ShoppingGoodsDtoList(items);


//        ShoppingGoodsListAndTotalDto list = new ShoppingGoodsListAndTotalDto(shoppingGoodsDtoList, shoppingGoodsDtoList.size());
//        PageInfo<ShoppingGoodsDto> pageInfo = new PageInfo<>(shoppingGoodsDtoList);
        PageInfo<Item> pageInfo = new PageInfo<>(items);
        int total = (int)pageInfo.getTotal();

        ShoppingGoodsListAndTotalDto list = new ShoppingGoodsListAndTotalDto(shoppingGoodsDtoList,total);


//塞到data中的是ShoppingGoodsListAndTotalDto
        return new ResponseUtil<>().setData(list);



    }
}
